<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="原始成绩" prop="originalScore">
        <el-input
          v-model="queryParams.originalScore"
          placeholder="请输入原始成绩"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入用户昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="科目名称" prop="subjectName">
        <el-input
          v-model="queryParams.subjectName"
          placeholder="请输入科目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="补考成绩" prop="resitScore">
        <el-input
          v-model="queryParams.resitScore"
          placeholder="请输入补考成绩"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请时间" prop="applyTime">
        <el-date-picker
          clearable
          v-model="queryParams.applyTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择申请时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="补考时间" prop="resitTime">
        <el-date-picker
          clearable
          v-model="queryParams.resitTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择补考时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="考试地点" prop="examLocation">
        <el-input
          v-model="queryParams.examLocation"
          placeholder="请输入考试地点"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否合格" prop="isQualified">
        <el-input
          v-model="queryParams.isQualified"
          placeholder="请输入是否合格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['resitExam:resitExam:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['resitExam:resitExam:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['resitExam:resitExam:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['resitExam:resitExam:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="resitExamList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="补考ID" align="center" prop="resitId" />
      <el-table-column label="原始成绩" align="center" prop="originalScore" />
      <el-table-column label="用户昵称" align="center" prop="nickName" />
      <el-table-column label="科目名称" align="center" prop="subjectName" />
      <el-table-column label="补考成绩" align="center" prop="resitScore" />
      <el-table-column
        label="申请时间"
        align="center"
        prop="applyTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="补考时间"
        align="center"
        prop="resitTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.resitTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="考试地点" align="center" prop="examLocation" />
      <el-table-column label="补考状态" align="center" prop="resitStatus">
        <template slot-scope="scope">
          <span>{{ getResitStatusText(scope.row.resitStatus) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否合格" align="center" prop="isQualified">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isQualified === '1'" type="success"
            >合格</el-tag
          >
          <el-tag v-else-if="scope.row.isQualified === '0'" type="danger"
            >不合格</el-tag
          >
          <span v-else>{{ scope.row.isQualified }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === '0'" type="success">正常</el-tag>
          <el-tag v-else-if="scope.row.status === '1'" type="danger"
            >异常</el-tag
          >
          <span v-else>{{ scope.row.status }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['resitExam:resitExam:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['resitExam:resitExam:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改补考管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="原始成绩" prop="originalScore">
          <el-input
            v-model="form.originalScore"
            placeholder="请输入原始成绩（必须小于60分）"
          />
        </el-form-item>
        <el-form-item label="用户ID" prop="userId">
          <el-select v-model="form.userId" placeholder="请选择用户">
            <el-option
              v-for="item in userOptions"
              :key="item.userId"
              :label="item.nickName"
              :value="item.userId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="科目ID" prop="subjectId">
          <el-select v-model="form.subjectId" placeholder="请选择科目">
            <el-option
              v-for="item in subjectOptions"
              :key="item.subjectId"
              :label="item.subjectName"
              :value="item.subjectId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="补考成绩" prop="resitScore">
          <el-input v-model="form.resitScore" placeholder="请输入补考成绩" />
        </el-form-item>
        <el-form-item label="申请时间" prop="applyTime">
          <el-date-picker
            clearable
            v-model="form.applyTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择申请时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="补考时间" prop="resitTime">
          <el-date-picker
            clearable
            v-model="form.resitTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择补考时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="考试地点" prop="examLocation">
          <el-input v-model="form.examLocation" placeholder="请输入考试地点" />
        </el-form-item>
        <el-form-item label="补考状态" prop="resitStatus">
          <el-select v-model="form.resitStatus" placeholder="请选择补考状态">
            <el-option label="待审核" value="0" />
            <el-option label="已通过" value="1" />
            <el-option label="已拒绝" value="2" />
            <el-option label="已完成" value="3" />
          </el-select>
        </el-form-item>
        <el-form-item label="是否合格" prop="isQualified">
          <el-select v-model="form.isQualified" placeholder="请选择是否合格">
            <el-option label="不合格" value="0" />
            <el-option label="合格" value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listResitExam,
  getResitExam,
  delResitExam,
  addResitExam,
  updateResitExam,
} from "@/api/resitExam/resitExam";
import { listUser } from "@/api/system/user";
import { listSubject } from "@/api/subject/subject";

export default {
  name: "ResitExam",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 补考管理表格数据
      resitExamList: [],
      // 用户选项
      userOptions: [],
      // 科目选项
      subjectOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        scoreId: null,
        userId: null,
        nickName: null,
        subjectId: null,
        subjectName: null,
        originalScore: null,
        resitScore: null,
        applyTime: null,
        resitTime: null,
        examLocation: null,
        resitStatus: null,
        isQualified: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "用户ID不能为空", trigger: "blur" },
        ],
        subjectId: [
          { required: true, message: "科目ID不能为空", trigger: "blur" },
        ],
        originalScore: [
          { required: true, message: "原始成绩不能为空", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              if (value === "" || value === null || value === undefined) {
                callback(new Error("请输入原始成绩"));
              } else if (parseFloat(value) >= 60) {
                callback(new Error("原始成绩必须小于60分才能参加补考"));
              } else {
                callback();
              }
            },
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getUserList();
    this.getSubjectList();
  },
  methods: {
    /** 查询补考管理列表 */
    getList() {
      this.loading = true;
      listResitExam(this.queryParams).then((response) => {
        this.resitExamList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 获取用户列表 */
    getUserList() {
      listUser().then((response) => {
        this.userOptions = response.rows;
      });
    },
    /** 获取科目列表 */
    getSubjectList() {
      listSubject().then((response) => {
        this.subjectOptions = response.rows;
      });
    },
    /** 获取补考状态文本 */
    getResitStatusText(status) {
      const statusMap = {
        0: "待审核",
        1: "已通过",
        2: "已拒绝",
        3: "已完成",
      };
      return statusMap[status] || status;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        resitId: null,
        scoreId: null,
        userId: null,
        subjectId: null,
        originalScore: null,
        resitScore: null,
        applyTime: null,
        resitTime: null,
        examLocation: null,
        resitStatus: null,
        isQualified: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.resitId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加补考管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const resitId = row.resitId || this.ids;
      getResitExam(resitId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改补考管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.resitId != null) {
            updateResitExam(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addResitExam(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const resitIds = row.resitId || this.ids;
      this.$modal
        .confirm('是否确认删除补考管理编号为"' + resitIds + '"的数据项？')
        .then(function () {
          return delResitExam(resitIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "resitExam/resitExam/export",
        {
          ...this.queryParams,
        },
        `resitExam_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
